package ar.Location.Api.Flickr;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

import org.json.JSONException;

import android.util.Log;
import ar.Location.Constant.Define;
import ar.Location.PlaceApi.AbsAPI;
import ar.Location.PlaceObject.Place;
import ar.globals.Globals;

import com.gmail.yuyang226.flickr.Flickr;
import com.gmail.yuyang226.flickr.FlickrException;
import com.gmail.yuyang226.flickr.REST;
import com.gmail.yuyang226.flickr.people.PeopleInterface;
import com.gmail.yuyang226.flickr.photos.Photo;
import com.gmail.yuyang226.flickr.photos.PhotoList;
import com.gmail.yuyang226.flickr.photos.PhotosInterface;
import com.gmail.yuyang226.flickr.photos.SearchParameters;
import com.gmail.yuyang226.flickr.photos.geo.GeoInterface;

public class FlickrAPI extends AbsAPI {

	public static Flickr flickr = null;

	public FlickrAPI() {
		initFlickrInstance();
	}
	
	public static PeopleInterface getPeopleInterface() {
		// TODO Auto-generated method stub
		if(flickr != null) {
			return flickr.getPeopleInterface();
		}
		return null;
	}
	
	public static GeoInterface getGeoInterface() {
		// TODO Auto-generated method stub
		if(flickr != null) {
			return flickr.getGeoInterface();
		}
		return null;
	}
	
	public static PhotosInterface getPhotosInterface() {
		// TODO Auto-generated method stub
		if(flickr != null) {
			return flickr.getPhotosInterface();
		}
		return null;
	}

	public void initFlickrInstance() {
		if (flickr == null) {
			try {
				flickr = new Flickr(Define.FLICKR_API_KEY,
						Define.FLICKR_API_SECRET, new REST(
								Define.FLICKR_API_HOST));
			} catch (ParserConfigurationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.d(TAG, e.getMessage());
			}
		}
	}

	@Override
	public ArrayList<Place> getListPlacesByGeoPoint(String latitude,
			String longitude, int radiusMetters) {
		// TODO Auto-generated method stub
		try {
			SearchParameters searchParams = new SearchParameters();
			searchParams.setText("a");
			searchParams.setSort(SearchParameters.DATE_POSTED_DESC);
			searchParams.setHasGeo(true);
			// params.setMinTakenDate(new Date(2012, 1, 1));
			int radius = (int) (radiusMetters / Define.KM);
			if (radius < 1) {
				radius = 1;
			}
			searchParams.setRadius(radius);
			searchParams.setRadiusUnits("km");
			searchParams.setLatitude(latitude);
			searchParams.setLongitude(longitude);
			
			PhotosInterface photoInterface = flickr.getPhotosInterface();
			PhotoList listPhotos = photoInterface.search(searchParams, Globals.MAX_NUMBER_OF_SEARCHED_LOCATIONS, 1);

			if (listPhotos != null && listPhotos.size() > 0) {

				ArrayList<Place> listPlaces = new ArrayList<Place>();

				int nPhotos = listPhotos.size();
				for (int i = 0; i < nPhotos; i++) {
					Place fPlace = getPlace(listPhotos.get(i));
					if (fPlace != null) {
					//	fPlace.index = i;
						listPlaces.add(fPlace);
					}
				}

				return listPlaces;
			}
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
			return null;
		} catch (FlickrException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
			return null;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.d(TAG, e.getMessage());
			return null;
		}
	}

	@Override
	public ArrayList<Place> getListPlacesByType(String latitude,
			String longitude, String type, int radiusMetters) {
		// TODO Auto-generated method stub
		return null;
	}

	private FlickrPlace getPlace(Photo photo) {
		// TODO Auto-generated method stub
		try {
			if (photo != null) {
				
				String name = photo.getTitle();
				if(name == null || name.length() == 0) {
					return null;
				}
				
				String imageUrl = photo.getThumbnailUrl();
				String websiteUrl = photo.getUrl();
				String views = String.valueOf(photo.getViews());
				String owner = "";
				if (photo.getOwner() != null) {
					owner = photo.getOwner().getId();
				}
				final String photoId = photo.getId();
				final String photoSecret = photo.getSecret();
				String dateTaken = "";
				if (photo.getDateTaken() != null) {
					dateTaken = photo.getDateTaken().toString();
				}

				final FlickrPlace fPlace = new FlickrPlace(Define.DS_FLICKR);

				if (name != null) {
					fPlace.name = name;
				}
				
				if (imageUrl != null) {
					fPlace.imageUrl = imageUrl;
				}

				if(photoId != null && photoId.length() > 0) {
					Thread threadLoadGeoData = new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							fPlace.updateGeoData(photoId, getGeoInterface());
						}
					});
					threadLoadGeoData.start();
					
					if(photoSecret != null && photoSecret.length() > 0) {
						Thread threadLoadDescription = new Thread(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								fPlace.updateDescription(photoId, photoSecret, getPhotosInterface());
							}
						});
						threadLoadDescription.start();
					}
				}

				if (websiteUrl != null) {
					fPlace.websiteUrl = websiteUrl;
				}

				if (views != null) {
					fPlace.views = views;
				}
				
				if (owner != null) {
					fPlace.user = owner;
					Thread threadLoadUserInfo = new Thread(new Runnable() {
						
						@Override
						public void run() {
							// TODO Auto-generated method stub
							fPlace.updateUserInfo(fPlace.user, getPeopleInterface());
						}
					});
					threadLoadUserInfo.start();
				}

				if (dateTaken != null) {
					fPlace.dateTime = dateTaken;
				}

				return fPlace;
			}
			return null;
		} catch (Exception e) {
			// TODO: handle exception
			Log.d(TAG, e.getMessage());
			return null;
		}
	}
}
